package com.xiguthings.waka.web.controller.usershare;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.*;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.service.cdcard.ICoolDownCardService;
import com.xiguthings.waka.service.sysconfig.ISysConfigService;
import com.xiguthings.waka.service.userCDcard.IUserCDcardService;
import com.xiguthings.waka.service.usershare.IUserShareService;
import com.xiguthings.waka.web.controller.base.AbstractController;
import com.xiguthings.waka.web.controller.base.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/usershare")
@Api(tags = "用户分享")
public class UserShareController extends AbstractController {
    @Resource
    private IUserShareService userShareService;
    @Resource
    private IUserCDcardService userCDcardService;
    @Resource
    private ICoolDownCardService coolDownCardService;

    @Resource
    private ISysConfigService sysConfigService;

    /**
     * 修改或者添加
     *
     * @param userShareVo
     * @return
     * @throws Exception
     */
    @ApiOperation(tags = {
            "1.控制台"}, value = "添加用户分享", httpMethod = "GET", notes = "(分享成功以后，获取加速卡)", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userShare.userId", value = "所属的用户的主键id", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userShare.shareTarget", value = "分享的目标", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping("/add")
    @ResponseBody
    public ResponseEntity add(@ApiIgnore UserShareVo userShareVo) throws Exception {
        if (userShareVo == null || userShareVo.getUserShare() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, null);
        }

        UserShare userShare = userShareVo.getUserShare();
        String shareTarget = userShare.getShareTarget();
        if (StringUtils.isBlank(shareTarget)) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, null);
        }


        User user = (User) super.getSession().getAttribute(SESSION_CRN_USER);
        String userId = user.getUserId();
        userShare.setUserId(userId);

        userShareService.insert(userShare);

        // 判断用户今天是否分享超过限制了
        // 先获取每天分享次数的限制
        int shareCount = 0;
        SysConfig sysConfig = sysConfigService.selectByCode(DataBaseStatus.CONFIG_CODE_USER_SHARE_COUNT);
        if (sysConfig != null) {
            shareCount = new Integer(sysConfig.getValue());
        }
        
        UserShareExample example = new UserShareExample();
        UserShareExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        int rows = userShareService.countByExample(example); // 查询用户已经分享的次数
        UserCoolDownCard userCdCard = null;
        if (rows < shareCount) {
            // 如果<于5了，则给予加速卡
            userCdCard = new UserCoolDownCard();
            userCdCard.setUserId(userId);
            int row = userCDcardService.insert(userCdCard);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userShareId", userShare.getUserId());
        String cdCardId = null;
        if (userCdCard != null) {
            cdCardId = userCdCard.getCoolDownCardId();
        }
        map.put("userCDcardId", cdCardId);
        return ajaxReturnMap(CODE_SUCCESS, "用户添加分享记录成功", map);
    }
}
